<template>
  <addBaseTeacherView ref="addBaseTeacherView" />
  <editBaseTeacherView ref="editBaseTeacherView" />
  <resetPassword ref="resetPassword" />
  <editBaseTeacherFaceUrlView ref="editBaseTeacherFaceUrlView" />
  <addBaseuploadEsign ref="addBaseuploadEsign" />
  <addFileView ref="addFileView" />
  <el-container style="min-height: calc(100vh - 70px);">
    <el-main>
      <div class="teacher">
        <div class="app-table">
          <div class="app-body">
            <div class="app-search">
              <el-form :inline="true" class="demo-form-inline" v-model="form">
                <el-form-item label="人员姓名">
                  <el-input placeholder="请输入人员姓名" v-model="form.name" @keyup.enter="searchEnter"></el-input>
                </el-form-item>
                <el-form-item label="人员工号">
                  <el-input placeholder="请输入人员工号" v-model="form.teacherNumber" @keyup.enter="searchEnter"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button class="serach_btn" type="primary" plain @click="onSubmit">搜索</el-button>
                  <el-button class="add_btn" type="success" plain @click="add">添加</el-button>
                  <el-button class="export_btn" type="warning" plain @click="exportExcel">导出报表</el-button>
                  <el-button class="export_btn1" type="warning" plain @click="updateExcel">批量更新</el-button>

                  <!-- <el-upload ref="upload" class="upload-demo" :limit="1" :auto-upload="false" :on-change="updateExcel"
                    :before-remove="handleRemove">
                    <template #trigger>
                      <el-button type="primary" class="export_btn" style="margin-right: 10px">批量更新</el-button>
                    </template>
</el-upload> -->
                </el-form-item>
              </el-form>
            </div>
            <el-table :data="list" size="default" style="width: 100%;" :row-class-name="tableRowClassName">
              <el-table-column prop="id" label="序号" width="60" />
              <el-table-column prop="name" label="人员姓名" />
              <el-table-column prop="teacherNumber" label="人员工号" />
              <el-table-column prop="departmentNames" label="处室/级部" />
              <el-table-column prop="subjectDepartments" label="学科" />
              <!-- <el-table-column
              prop="sex"
              label="性别"
              :formatter="formatterSex"
            /> -->
              <el-table-column prop="mobile" label="联系电话">
                <template #default="scope">
                  <el-button v-if="this.selectData.id != scope.row.id" class="retire_btn" type="success"
                    @click="getMobile(scope.row)">查看</el-button>
                  <p style="margin: 0;" v-if="this.selectData.id == scope.row.id">{{ mobileNumber }}</p>
                </template>
              </el-table-column>
              <el-table-column prop="remark" label="备注" width="200" />

              <el-table-column label="操作" width="520px">

                <template #default="scope">
                  <el-button class="face_btn" type="warning" @click="editFaceUrl(scope.row)">人脸照片</el-button>
                  <el-button class="edit_btn" type="success" @click="editApp(scope.row)">编辑</el-button>
                  <el-button class="edit_btn" type="success" @click="resetApp(scope.row)">重置密码</el-button>
                  <el-button class="edit_btn" type="success" @click="uploadEsign(scope.row)">上传签名</el-button>
                  <el-button class="del_btn" type="danger" @click="deleteApp(scope.row.id)">删除</el-button>
                  <el-button class="retire_btn" v-if="scope.row.personnelType == '01'" type="success"
                    @click="editDelTeacher(scope.row)">退休</el-button>
                </template>
              </el-table-column>

              <template v-slot:empty>
                <el-empty description="暂无数据"></el-empty>
              </template>
            </el-table>
            <el-config-provider :locale="locale">
              <el-pagination :total="total" :page-size="pageSize" :current-page="page"
                :page-sizes="[5, 10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper"
                @size-change="changePageSize" @current-change="changeCurrentPage">
              </el-pagination>
            </el-config-provider>
          </div>
          <bottomView />
        </div>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import resetPassword from "@/components/baseInfo/resetPassword.vue";
import addBaseTeacherView from "@/components/baseInfo/addBaseTeacher.vue";
import editBaseTeacherView from "@/components/baseInfo/editBaseTeacher.vue";
import addBaseuploadEsign from "@/components/baseInfo/addBaseuploadEsign.vue";
import editBaseTeacherFaceUrlView from "@/components/baseInfo/editBaseTeacherFaceUrl.vue";
import bottomView from "@/components/bottom.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture } from "@element-plus/icons-vue";
import { getCurrentInstance } from "vue";
import { decrypt } from "@/utils/dateFormat";
import { API_BASE_URL } from "../../constants"
import addFileView from "@/components/addExcelFile.vue";

export default {
  name: "BaseTeacherList",

  data() {
    return {
      type: 0,
      title: "",
      page: 0,
      pageSize: 10,
      total: 0,
      limit: 10,
      list: [],
      flagId: '',
      schoolNames: [],
      form: {
        name: "",
        teacherNumber: "",
      },
      selectData: {},
      mobileNumber: '',
    };
  },
  components: {
    addBaseTeacherView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage, addFileView,
    bottomView, editBaseTeacherView, editBaseTeacherFaceUrlView, resetPassword, addBaseuploadEsign
  },

  setup() {
    const instance = getCurrentInstance()
    let schoolNames = instance.appContext.config.globalProperties.$schoolNames
    return {
      locale: zhCn,
      schoolNames: schoolNames
    }
  },
  mounted() {
    this.typeChange();
    this.getBaseTeacherListApp();
  },
  watch: {
    $route() {
      this.page = 1
      this.typeChange();
      this.getBaseTeacherListApp();
    },
  },
  methods: {
    searchEnter: function (e) {
      console.log(e);
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.getBaseTeacherListApp();
      }
    },
    getMobile(data) {
      this.selectData = data
      this.mobileNumber = ''
      this.getBasedecrypt(this.selectData)
    },
    getBasedecrypt(object) {
      let params = new FormData()
      params.append('encryptedData', object.mobile)
      httpService("/javaApi/crypto/decrypt", params, "post").then(
        (data) => {
          this.mobileNumber = data
          console.log(this.mobileNumber);
        }
      );
    },
    getDDDD(i, j, k, g) {
      console.log(i)
      console.log(j)

      console.log(k)

      console.log(g)

    },
    formatterSex(row, column) {
      return row.sex == "1" ? "男" : "女";
    },
    add() {
      this.$refs.addBaseTeacherView.title = "添加" + this.title
      this.$refs.addBaseTeacherView.personnelType = this.type
      this.$refs.addBaseTeacherView.dialogFormVisible = true;
      this.$refs.addBaseTeacherView.getBaseRole()
    },
    editApp(obj) {
      this.$refs.editBaseTeacherView.title = "修改信息"
      this.$refs.editBaseTeacherView.schoolNames = this.schoolNames
      this.$refs.editBaseTeacherView.getBaseTeacherById(obj.id);
      this.$refs.editBaseTeacherView.getBaseRole();
      this.$refs.editBaseTeacherView.dialogFormVisible = true;
    },
    resetApp(obj) {
      this.$refs.resetPassword.title = "重置密码"
      this.$refs.resetPassword.schoolNames = this.schoolNames
      // this.$refs.resetPassword.getBaseTeacherById(obj.id);
      this.$refs.resetPassword.teacherId = obj.id;
      this.$refs.resetPassword.dialogFormVisible = true;
    },
    uploadEsign(obj) {
      this.$refs.addBaseuploadEsign.title = "上传签名"
      this.$refs.addBaseuploadEsign.schoolNames = this.schoolNames
      // this.$refs.addBaseuploadEsign.getBaseTeacherById(obj.id);
      this.$refs.addBaseuploadEsign.id = obj.id;
      this.$refs.addBaseuploadEsign.dialogFormVisible = true;
      this.$refs.addBaseuploadEsign.getEsign()
    },
    editFaceUrl(obj) {
      this.$refs.editBaseTeacherFaceUrlView.title = "人脸照片"
      this.$refs.editBaseTeacherFaceUrlView.getBaseTeacherById(obj.id);
      this.$refs.editBaseTeacherFaceUrlView.dialogFormVisible = true;
    },
    deleteApp(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService("/javaApi/baseTeacher/deleteById", "id=" + id, "post").then(
            (data) => {
              if (data.code == 200) {
                ElMessage.success("删除成功");
                this.getBaseTeacherListApp();
              } else {
                ElMessage.error(data.message);
              }
            }
          );
        })
        .catch(() => { });
    },
    editDelTeacher(data) {
      ElMessageBox.confirm("确定要退休吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let params = new FormData();
        params.append("id", data.id);
        params.append("personnelType", "02");
        params.append("rId", 84);
        httpService("/javaApi/baseTeacher/edit", params, "post").then(
          (data) => {
            if (data.code == 200) {
              ElMessage.success("删除成功");
              this.getBaseTeacherListApp();
            } else {
              ElMessage.error(data.message);
            }
          }
        );
      })
        .catch(() => { });
      // console.log(data);

    },
    exportExcel() {
      httpService(
        "/javaApi/baseTeacher/exportBaseTeacher",
        {
          name: this.form.name,
          teacherNumber: this.form.teacherNumber,
          personnelType: this.type,
        },
        "get"
      ).then((data) => {
        if (data.code == 200) {
          data.object = data.object.replace("http://wx.zhengzhong.cn/javaApi", API_BASE_URL)

          window.open(data.object, "_blank");
        }
      });
    },
    updateExcel(file) {
      this.$refs.addFileView.dialogFormVisible = true;
      this.$refs.addFileView.title = "批量更新人员信息";
      this.$refs.addFileView.uploadApiUrl = "/javaApi/baseTeacher/updateBatch";
    },
    onSubmit() {
      this.getBaseTeacherListApp();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;

      this.getBaseTeacherListApp();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getBaseTeacherListApp();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    getBaseTeacherListApp() {
      httpService(
        "/javaApi/baseTeacher/list/" + this.page + "/" + this.pageSize,
        {
          name: this.form.name,
          teacherNumber: this.form.teacherNumber,
          personnelType: this.type,
        },
        "get"
      ).then((data) => {
        this.list = data.list;
        this.total = data.total;
        this.pageCount = data.pages;
      });
    },
    typeChange() {
      this.type = this.$route.query.type;
    },
  },
};
</script>

<style scoped>
.el-main {
  padding: 0px;
  text-align: left
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 5px 10px 10px;
}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  background: white;
  padding: 10px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 244px);
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}

.serach_btn {
  background-color: #007EFF;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.add_btn {
  background-color: #00c78c;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.edit_btn {
  background-color: #fff;
  color: #00c78c;
  border: none;
}

.del_btn {
  background-color: #fff;
  color: #FF0000;
  border: none;
}

.face_btn {
  background-color: #fff;
  color: #ff8400;
  border: none;
}

.retire_btn {
  background-color: #fff;
  color: #007EFF;
  border: none;
}

.export_btn {
  background-color: #FF7200;
  border-radius: 8px;
  color: #fff;
  border: none;
}

.export_btn1 {
  background-color: #909399;
  border-radius: 8px;
  color: #fff;
  border: none;
}
</style>
